package com.macro.mall.tiny.modules.pms.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.macro.mall.tiny.common.api.CommonResult;
import com.macro.mall.tiny.modules.pms.model.PmsControlSkill;
import com.macro.mall.tiny.modules.pms.model.PmsControlSkillBrand;
import com.macro.mall.tiny.modules.pms.service.PmsControlSkillBrandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 工控技术品牌表 前端控制器
 * </p>
 *
 * @author tyrone
 * @since 2025-03-02
 */
@RestController
@RequestMapping("/pms/pmsControlSkillBrand")
@Tag(name = "PmsControlSkillBrandController", description = "工控技术品牌表")
@Api(tags = "PmsControlSkillBrandController")
public class PmsControlSkillBrandController {

    @Autowired
    private PmsControlSkillBrandService pmsControlSkillBrandService;

    @PostMapping("/page")
    @ApiOperation("分页查询")
    public CommonResult<Page<PmsControlSkillBrand>> page(@RequestBody PmsControlSkillBrand pmsControlSkillBrand) {
        Page<PmsControlSkillBrand> page = new Page<>(pmsControlSkillBrand.getPageNum(), pmsControlSkillBrand.getPageSize(), true);
        QueryWrapper<PmsControlSkillBrand> queryWrapper = new QueryWrapper<>();
        if (pmsControlSkillBrand.getId() != null) {
            queryWrapper.eq("id", pmsControlSkillBrand.getId());
        }
        if (pmsControlSkillBrand.getName() != null) {
            queryWrapper.like("name", pmsControlSkillBrand.getName());
        }
        if (pmsControlSkillBrand.getSort() != null) {
            queryWrapper.eq("sort", pmsControlSkillBrand.getSort());
        }
        if (pmsControlSkillBrand.getFirstLetter() != null) {
            queryWrapper.eq("first_letter", pmsControlSkillBrand.getFirstLetter());
        }
        if (pmsControlSkillBrand.getStatus() != null) {
            queryWrapper.eq("status", pmsControlSkillBrand.getStatus());
        }
        return CommonResult.success(pmsControlSkillBrandService.page(page, queryWrapper));
    }


    @PostMapping("/save")
    @ApiOperation("新增")
    public CommonResult<Boolean> save(@RequestBody PmsControlSkillBrand pmsControlSkillBrand) {
        List<PmsControlSkillBrand> list = pmsControlSkillBrandService.list(new QueryWrapper<PmsControlSkillBrand>().eq("name", pmsControlSkillBrand.getName()));
        if (list.size() > 0) {
            return CommonResult.failed("名称已存在");
        }
        return CommonResult.success(pmsControlSkillBrandService.save(pmsControlSkillBrand));
    }

    @PostMapping("/update")
    @ApiOperation("更新")
    public CommonResult<Boolean> update(@RequestBody PmsControlSkillBrand pmsControlSkillBrand) {
        return CommonResult.success(pmsControlSkillBrandService.updateById(pmsControlSkillBrand));
    }

    @PostMapping("/delete")
    @ApiOperation("删除")
    public CommonResult<Boolean> batchDelete(@RequestBody List<Integer> ids){
        return CommonResult.success(pmsControlSkillBrandService.removeByIds(ids));
    }


}

